#include "Class.h"
#include "ClassContent.h"

namespace FunctionViewer {
	void Class::Init(Namespace* f_namespace, unsigned long f_id, char* v_name) {
		program = f_namespace->program;
		v_namespace = f_namespace;
		prev = f_namespace->last_class;
		next = NULL;
		if (prev == NULL) f_namespace->first_class = this;
		else prev->next = this;
		f_namespace->last_class = this;
		id = f_id;

		content_counter = 0;
		contentcount = 0;
		first_content = NULL;
		last_content = NULL;
		current_content = NULL;

		++f_namespace->classcount;
		cname = v_name;

	}

	void Class::DeInit() {
		//Remove from linked list
		if (prev == NULL) v_namespace->first_class = next;
		else prev->next = next;
		if (next == NULL) v_namespace->last_class = prev;
		else next->prev = prev;
		if (v_namespace->current_class == this) v_namespace->current_class = NULL;

		--v_namespace->classcount;
	}

	ClassContent* Class::CreateContent(char* type, char* func_name, char* description) {
		ClassContent* f_classcontent;

		//Allocate memory.
		f_classcontent = (ClassContent*)(malloc(sizeof(ClassContent)));
		if (f_classcontent == NULL) {
			printf("Memory allocation faild! Could not locate memory for classcontent!");
			return NULL;
		}
		f_classcontent->Init(this, ++content_counter, type, func_name, description);

		return f_classcontent;
	}
}
